import visitMonthPlanModel from "../model/visitMonthPlanModel";
import visitMonthPlanModel2 from "../model2/visitMonthPlanModel2";
import addExportModel from '../model/addExportModel';
import addExportModel2 from '../model2/addExportModel2';
import approvalModel from "../model/approvalModel";
import approvalModel2 from "../model2/approvalModel2";
import consumerRecoderModel from "../model/consumerRecoderModel";
import consumerRecoderModel2 from "../model2/consumerRecoderModel2";
import storeRecoderModel from "../model/storeRecoderModel";
import storeRecoderModel2 from "../model2/storeRecoderModel2";
import excelExportModel from '../model/excelExportModel';
import excelExportModel2 from '../model2/excelExportModel2';
import storeInfoModel from "../model/storeInfoModel";
import storeInfoModel2 from "../model2/storeInfoModel2";
import userCheckInModel from "../model/userCheckInModel";
import userCheckInModel2 from "../model2/userCheckInModel2";
import emplModel2 from "../model2/emplModel";
import emplModel from "../model/empl2Model";
import temporaryDataModel from "../model/temporaryDataModel";
import temporaryDataModel2 from "../model2/temporaryModel2";
import * as moment from 'moment';
import dTalkApi from '../core/dTalkApi';
import * as crypto from 'crypto';
import { DTalkCrypt } from 'dingtalk_isv';
import * as PKCS7Encoder  from 'dingtalk_isv/dingtalk/PKCS7Encoder';
import * as rp from 'request-promise';

//一个数据库服务器----备份数据到另外一个数据库服务器

class TestService{
    constructor(){}

    //visitMonthPlan  拜访计划
    async backupMonthPlan(){
        /* let dele=await visitMonthPlanModel2.remove({})
        console.log(dele,'dele_dele_dele_dele_dele_dele_dele_') */
        let data=await visitMonthPlanModel.find({})
        let data2=await visitMonthPlanModel2.find({})
       console.log(data.length,'????????????',data2.length)
       /*  
        if(data.length>0){
            for (let index = 0; index < data.length; index++) {
                let element = data[index];
                let b=await  visitMonthPlanModel2.create({
                    sourceId: element.sourceId,
                    userId: element.userId,
                    user:element.user,
                    avatar:element.avatar,
                    jobnumber: element.jobnumber,
                    deptId: element.deptId,
                    provinceManager: element.provinceManager,
                    areaManager: element.areaManager,
                    modelManager: element.modelManager,      //模块负责人
                    nationalManager: element.nationalManager,//全国负责人
                    city: element.city,
                    roles: element.roles,     //角色
                    province: element.province,//员工所在省份
                    area: element.area,//员工所在区域
                    manageModel: element.manageModel,     //员工所在模块
                    team: element.team,//员工所在团队
                    nation:element.nation,
                    provinceName: element.provinceName,//省份名称
                    areaName: element.areaName,//区域名称
                    manageModelName: element.manageModelName,//模块名称
                    teamName:element.teamName,//团队名称
                    nationName: element.nationName,//全国名称
                    month: element.month,
                    visitRecoder: element.visitRecoder,
                    channel: element.channel,
                    mark: element.mark,
                    planDate: element.planDate,
                    consumerName: element.consumerName,
                    consumerCode: element.consumerCode,
                    rest: element.rest,
                    meeting: element.meeting,
                    plantype: element.plantype,
                    planStatus: element.planStatus,
                    customerType:element.customerType,
                    purpose: element.purpose,
                    comment: element.comment,
                    cancelApply: element.cancelApply,
                    remark: element.remark,
                    leadIndate:element.leadIndate
                });
                console.log(b._id,'<<<<<<<<<<<<<<',b.planDate,b.userId,'////////////',index)
                
            }
        }   */
        console.log('1111111111111')
    }

    //addExport
    async backupaddEXport(){
        /* let dele=await addExportModel2.remove({})
        console.log(dele,'dele_dele_dele_dele_dele_dele_dele_') */
        let data=await addExportModel.find({})
        let data2=await addExportModel2.find({})
        console.log(data.length,'????????????',data2.length)
        /* 
        if(data.length>0){
            for (let index = 0; index < data.length; index++) {
                let a = data[index];
                let b=await addExportModel2.create({
                    name_id:a.name_id,
                    name_:a.name_,
                    date_:a.date_,
                    month:a.month,
                    consumerCount_day:a.consumerCount_day,//日均
                    consumerCount_empl:a.consumerCount_empl,//人均
                    comment:a.comment
                })
                console.log(b,'<<<<<<<<<<<<<<',index)
            }
        }    */
        console.log('添加结束!!!!!!!!!')
    }

    //consumerRecoder
    async backupVisitConsumerRecoder2(){
        /* let dele=await consumerRecoderModel2.remove({})
        console.log(dele,'dele_dele_dele_dele_dele_dele_dele_') */
        let data=await consumerRecoderModel.find({})
        let data2=await consumerRecoderModel2.find({})
        console.log(data.length,'????????????',data2.length)
     /* 
        if(data.length>0){
            for (let index = 0; index < data.length; index++) {
                let a = data[index];
                let b=await consumerRecoderModel2.create({
                    sourceId: a.sourceId,
                    planId: a.planId,
                  
                    userid:a.userid,
                    jobnumber: a.jobnumber,
                    userName: a.userName,
                    
                    consumerName: a.consumerName,
                    consumerCode: a.consumerCode,
                    city: a.city,
                    cityType: a.cityType,
                    typeOne: a.typeOne,
                    typeTwo:a.typeTwo,
                    typeThree: a.typeThree,
                    typeFour: a.typeFour,
                    
                    typeFive: a.typeFive,
                    typeSix: a.typeSix,
                    typeSeven: a.typeSeven,
                    typeEight: a.typeEight,
                    typeNine: a.typeNine,
                    typeTen: a.typeTen,

                    comment:a.comment,

                    timestamp: a.timestamp,
                    month: a.month,
                    date:a.date,
                    planDate:a.planDate,
                    planType: a.planType,
                    consumerType: a.consumerType,
                    vistitWay: a.vistitWay,
                    position: a.position,
                    isChuchai: a.isChuchai,
                    picture: a.picture,
                    purpose:a.purpose,
                    talkingPerson:a.talkingPerson,
                    job: a.job,
                    connection: a.connection,
                    visitResult: a.visitResult,
                    followMatter: a.followMatter,
                   
                    cooperation: a.cooperation 
                })
                console.log(b,'<<<<<<<<<<<<<<',index)
            }
        }   */
        console.log('添加结束!!!!!!!!!')
    }

    //导出excel报表的数据
    async backupExcelExport2(){
        /* let dele=await excelExportModel2.remove({})
        console.log(dele,'dele_dele_dele_dele_dele_dele_dele_') */
        let data=await excelExportModel.find({})
        let data2=await excelExportModel2.find({})
        console.log(data.length,'????????????',data2.length)
     /* 
        if(data.length>0){
            for (let index = 0; index < data.length; index++) {
                let a = data[index];
                let b=await excelExportModel2.create({
                     userId:a.userId,
                    userName:a.userName,
                    date_:a.date_,
                    visitCount_plan:a.visitCount_plan,//计划拜访客户数量(一天)
                    actuall_visitCount:a.actuall_visitCount,//实际拜访数量
                    consumerRate:a.consumerRate, //客户拜访达成率(一天)

                    newConsumerRate:a.newConsumerRate, //新客户拜访达成率
                    newConsumerCount_plan:a.newConsumerCount_plan,//计划拜访新客户数量
                    newConsumerCount_visit:a.newConsumerCount_visit,//实际拜访新客户数量

                    checkInRate:a.checkInRate,//签到率
                    actuall_checkIn:a.actuall_checkIn,//实际签到天数
                    checkInTime:a.checkInTime,

                    unBase_check:a.unBase_check,

                    reachStandardRate:a.reachStandardRate,//已拜访门店达标率
                    reachStandardStoreCount:a.reachStandardStoreCount,//门店达标的数量

                    visitedStoreCount:a.visitedStoreCount,//已经拜访的门店数量

                    storeSchadule:a.storeSchadule,//门店拜访进度
                    totalCountStore:a.totalCountStore,//员工名下总的门店数量


                    year:a.year,
                    month: a.month,
                    comment:a.comment
                })
                console.log(b,'<<<<<<<<<<<<<<',index)
            }
        }   */
        console.log('添加结束!!!!!!!!!')
    }

    //storeInfo 门店信息
    async backupStoreInfo2(){
        /* let dele=await storeInfoModel2.remove({})
        console.log(dele,'dele_dele_dele_dele_dele_dele_dele_') */
        let data=await storeInfoModel.find({})
        let data2=await storeInfoModel2.find({})
        console.log(data.length,'????????????',data2.length)
         /* 
        if(data.length>0){
            for (let index = 0; index < data.length; index++) {
                let bill = data[index];
                let b=await storeInfoModel2.create({
                    sourceId: bill.sourceId,
                    storeCode: bill.storeCode,//门店编码
                    storeName: bill.storeName,//门店名称
                    store_classification: bill.store_classification,//门店类型
                    channel: bill.channel,//渠道
                    contact_name: bill.contact_name,//联系人姓名
                    phone: bill.phone,//电话
                    consumerCode: bill.consumerCode,//客户编码
                    consumerName: bill.consumerName,//客户名称
                    city: bill.city,//城市
                    cityType: bill.cityType,//城市级别
                    store_address: bill.store_address,//门店地址
                    iT_Mall_name: bill.iT_Mall_name,//IT Mall name
                    iT_Mall_address: bill.iT_Mall_address,//IT Mall address                                      
                })
                console.log(b,'<<<<<<<<<<<<<<',index)
            }
        }    */
        console.log('添加结束!!!!!!!!!')
    }

    //门店拜访记录
    async backupStoreRecoder2(){
       /*  let dele=await storeRecoderModel2.remove({})
        console.log(dele,'dele_dele_dele_dele_dele_dele_dele_') */
        // let data=await storeRecoderModel.find({month:'10'})
        // let data2=await storeRecoderModel2.find({month:'10'})
        let data=await storeRecoderModel.find({})
        let data2=await storeRecoderModel2.find({})
        console.log(data.length,'????????????',data2.length)
       /*  if(data.length>0){
            for (let index = 0; index < data.length; index++) {
                let res = data[index];
                let b=await storeRecoderModel2.create({
               
                        sourceId: res.sourceId,
                        planId: res.planId,
                        timestamp: res.timestamp,
                        userid: res.userid,//员工的userid
                        month: res.month,
                        date: res.date,
                        planDate: res.planDate,
                        planType: res.planType,
                        storeName: res.storeName,
                        storeCode: res.storeCode,
                        storeType: res.storeType,//示范网吧  模范店等
                        position: res.position,
                        isChuchai: res.isChuchai,//是否出差
                        isListSample: res.isListSample,
                        sampleList: res.sampleList,
                        isListStandard: res.isListStandard,
                        competitor: res.competitor,
                        storeActivity: res.storeActivity,
                        pictureStoreActivity: res.pictureStoreActivity,
                        picture: res.picture,//门头照片
                        isListLetter: res.isListLetter,
                        listLetter: res.listLetter,
                        information: res.information
                                                     
                })
                console.log(b,'<<<<<<<<<<<<<<',index)
            }
        }    */
        console.log('添加结束!!!!!!!!!')
    }

    //签到率
    async backuUserCheckin2(){
       /*  let dele=await userCheckInModel2.remove({})
        console.log(dele,'dele_dele_dele_dele_dele_dele_dele_') */
        let data=await userCheckInModel.find({})
        let data2=await userCheckInModel2.find({})
        console.log(data.length,'????????????',data2.length)
        /* 
        if(data.length>0){
            for (let index = 0; index < data.length; index++) {
                let e = data[index];
                let b=await userCheckInModel2.create({
                    sourceId:e.sourceId,
                    date_:e.date_,//签到日期
                    month:e.month,//签到月份
                    userId:e.userId,//员工id,
                    checkin_time:e.checkin_time,//员工签到的时间
                    isCheckInNineAm:e.isCheckInNineAm,//是否是上午9:00前签到
                    time_getCheckData:e.time_getCheckData//签到数据 获取的时间   
                                                     
                })
                console.log(b,'<<<<<<<<<<<<<<',index)
            }
        }    */
        console.log('添加结束!!!!!!!!!')
    }

    //同步钉钉人员信息
    async backuEmpl2(){
        /* let dele=await emplModel2.remove({})
        console.log(dele,'dele_dele_dele_dele_dele_dele_dele_') */
        let data=await emplModel.find({})
        let data2=await emplModel2.find({})
        console.log(data.length,'????????????',data2.length)
        /* 
        if(data.length>0){
            for (let index = 0; index < data.length; index++) {
                let e = data[index];
                let b=await emplModel2.create({
                  
                    userId:e.userId,//员工id,
                    
                    status:e.status,
                    
                    userName:e.userName,    //用户名称
                    avatar:e.avatar,      //用户头像
                    mobile: e.mobile,     //用户电话
                    deptId:e.deptId,           //用户所在部门id
                    // roles:[],           //用户角色
                    userArea:e.userArea,    //用户所在区域-----------工作地
                    isLeader:e.isLeader,   //是否是部门主管
                    isAdmin:e.isAdmin,    //是否是企业管理员
                    isBoss:e.isBoss,     //是否是企业老板
                    userEmail:e.userEmail,   //用户邮箱
                    jobnumber:e.jobnumber,   //用户工号
                    job:e.job,            //工作职位
                    comment:e.comment
                                                     
                })
                console.log(b,'<<<<<<<<<<<<<<',index)
            }
        }    */
        console.log('添加结束!!!!!!!!!')
    }

    //数据暂存
    async backuTempdata(){
       /*  let dele=await temporaryDataModel2.remove({})
        console.log(dele,'dele_dele_dele_dele_dele_dele_dele_') */
        let data=await temporaryDataModel.find({})
        let data2=await temporaryDataModel2.find({})
        console.log(data.length,'????????????',data2.length)
        /* 
        if(data.length>0){
            for (let index = 0; index < data.length; index++) {
                let res = data[index];
                let b=await temporaryDataModel2.create({
                        month: res.month,
                        number:res.number,
                        userId:res.userId,//员工id
                        jobnumber:res.jobnumber,//员工工号
                        plantype:res.plantype,//拜访类型
                        planPurpose:res.planPurpose,//计划的拜访目的
                        name:res.name,//名称
                        code:res.code,//编码
                        date:res.date,//拜访日期
                        location:res.location,//签到地址   
                        pic:res.pic,//图片
                        way:res.way,//拜访方式
                        recoderPurpose:res.recoderPurpose,
                        talkPersonName:res.talkPersonName,
                        talkPersonJob:res.talkPersonJob,
                        connection:res.connection,
                        visitResult:res.visitResult,
                        followMatter:res.followMatter,
                        cooperation:res.cooperation,
                    
                        //门店
                        sampleList:res.sampleList,//样机列表[]
                    
                        storeType:res.storeType,
                        isListSample:res.isListSample,
                       
                        isListStandard:res.isListStandard,
                        competitor:res.competitor,
                        storeActivity:res.storeActivity,
                        pictureStoreActivity:res.pictureStoreActivity,
                        listLetter:res.listLetter,
                        information:res.information
                                                     
                })
                console.log(b,'<<<<<<<<<<<<<<',index)
            }
        }  */
        console.log('添加结束!!!!!!!!!')       
    }

    async test(){
      //await this.plan_consumerRecoder()
    }
    

    async plan(){
        let date = moment().format('YYYY-MM-DD');
        let data2=await visitMonthPlanModel2.distinct('_id',{planDate:date});
        if(data2.length>0){
            for (let index = 0; index < data2.length; index++) {
                let ele = data2[index];
                //console.log(ele,';;;;;;;;;',index)
              let dele=  await visitMonthPlanModel2.remove({_id:ele})
                console.log(dele,'<---',index)
            }
        }
        let data=await visitMonthPlanModel.find({planDate:date});
        if(data.length>0){
            for (let index = 0; index < data.length; index++) {
                let element = data[index];
                let b=await  visitMonthPlanModel2.create({
                    sourceId: element.sourceId,
                    userId: element.userId,
                    user:element.user,
                    avatar:element.avatar,
                    jobnumber: element.jobnumber,
                    deptId: element.deptId,
                    provinceManager: element.provinceManager,
                    areaManager: element.areaManager,
                    modelManager: element.modelManager,      //模块负责人
                    nationalManager: element.nationalManager,//全国负责人
                    city: element.city,
                    roles: element.roles,     //角色
                    province: element.province,//员工所在省份
                    area: element.area,//员工所在区域
                    manageModel: element.manageModel,     //员工所在模块
                    team: element.team,//员工所在团队
                    nation:element.nation,
                    provinceName: element.provinceName,//省份名称
                    areaName: element.areaName,//区域名称
                    manageModelName: element.manageModelName,//模块名称
                    teamName:element.teamName,//团队名称
                    nationName: element.nationName,//全国名称
                    month: element.month,
                    visitRecoder: element.visitRecoder,
                    channel: element.channel,
                    mark: element.mark,
                    planDate: element.planDate,
                    consumerName: element.consumerName,
                    consumerCode: element.consumerCode,
                    rest: element.rest,
                    meeting: element.meeting,
                    plantype: element.plantype,
                    planStatus: element.planStatus,
                    customerType:element.customerType,
                    purpose: element.purpose,
                    comment: element.comment,
                    cancelApply: element.cancelApply,
                    remark: element.remark,
                    leadIndate:element.leadIndate
                });
                console.log(b._id,'<<<<<<<<<<<<<<',b.planDate,b.userId,'////////////',index)
            }
        }
        let num=await visitMonthPlanModel.count({planDate:date})
        let num2=await visitMonthPlanModel2.count({planDate:date})
        console.log(num,'<---',num2)
    }

    async store_recoder(){
        let date = moment().format('YYYY-MM-DD');
        let data2=await storeRecoderModel2.distinct('_id',{planDate:date});
        if(data2.length>0){
            for (let index = 0; index < data2.length; index++) {
                let ele = data2[index];
                //console.log(ele,';;;;;;;;;',index)
              let dele=  await storeRecoderModel2.remove({_id:ele})
                console.log(dele,'<---',index)
            }
        }
        let data=await storeRecoderModel.find({planDate:date});
        if(data.length>0){
            for (let index = 0; index < data.length; index++) {
                let res = data[index];
                let b=await storeRecoderModel2.create({
                    sourceId: res.sourceId,
                    planId: res.planId,
                    timestamp: res.timestamp,
                    userid: res.userid,//员工的userid
                    month: res.month,
                    date: res.date,
                    planDate: res.planDate,
                    planType: res.planType,
                    storeName: res.storeName,
                    storeCode: res.storeCode,
                    storeType: res.storeType,//示范网吧  模范店等
                    position: res.position,
                    isChuchai: res.isChuchai,//是否出差
                    isListSample: res.isListSample,
                    sampleList: res.sampleList,
                    isListStandard: res.isListStandard,
                    competitor: res.competitor,
                    storeActivity: res.storeActivity,
                    pictureStoreActivity: res.pictureStoreActivity,
                    picture: res.picture,//门头照片
                    isListLetter: res.isListLetter,
                    listLetter: res.listLetter,
                    information: res.information
                                                    
                })
                console.log(b,'<<<<<<<<<<<<<<',index)
            }
        }

        let num=await storeRecoderModel.count({planDate:date})
        let num2=await storeRecoderModel2.count({planDate:date})
        console.log(num,'<---',num2)
    }

    async con_recoder(){
        let date = moment().format('YYYY-MM-DD');
        let data2=await consumerRecoderModel2.distinct('_id',{planDate:date,comment:2});
        if(data2.length>0){
            for (let index = 0; index < data2.length; index++) {
                let ele = data2[index];
                //console.log(ele,';;;;;;;;;',index)
              let dele=  await consumerRecoderModel2.remove({_id:ele})
                console.log(dele,'<---',index)
            }
        }
        let data=await consumerRecoderModel.find({planDate:date,comment:2});
        if(data.length>0){
            for (let index = 0; index < data.length; index++) {
                let a = data[index];
                let b=await consumerRecoderModel2.create({
                    sourceId: a.sourceId,
                    planId: a.planId,
                    userid:a.userid,
                    jobnumber: a.jobnumber,
                    userName: a.userName,
                    consumerName: a.consumerName,
                    consumerCode: a.consumerCode,
                    city: a.city,
                    cityType: a.cityType,
                    typeOne: a.typeOne,
                    typeTwo:a.typeTwo,
                    typeThree: a.typeThree,
                    typeFour: a.typeFour,
                    typeFive: a.typeFive,
                    typeSix: a.typeSix,
                    typeSeven: a.typeSeven,
                    typeEight: a.typeEight,
                    typeNine: a.typeNine,
                    typeTen: a.typeTen,

                    comment:a.comment,

                    timestamp: a.timestamp,
                    month: a.month,
                    date:a.date,
                    planDate:a.planDate,
                    planType: a.planType,
                    consumerType: a.consumerType,
                    vistitWay: a.vistitWay,
                    position: a.position,
                    isChuchai: a.isChuchai,
                    picture: a.picture,
                    purpose:a.purpose,
                    talkingPerson:a.talkingPerson,
                    job: a.job,
                    connection: a.connection,
                    visitResult: a.visitResult,
                    followMatter: a.followMatter,
                   
                    cooperation: a.cooperation 
                })
                console.log(b,'<<<<<<<<<<<<<<',index)
            }
        }

        let num=await consumerRecoderModel.count({planDate:date,comment:2})
        let num2=await consumerRecoderModel2.count({planDate:date,comment:2})
        console.log(num,'<---',num2)
    }


    async plan_storeRecoder(){
        let monthList=await storeRecoderModel.distinct('month',{});
        console.log(monthList,'<------monthList');
        let r_data2=await storeRecoderModel.find({month:'10',planDate:{"$lte": '2018-10-24'}});//10月份的另外
        let unplanList:any[]=[]//有记录没有对应的计划的
        if(r_data2.length>0){
            for (let index = 0; index < r_data2.length; index++) {
                let m = r_data2[index];
                let c_data=await visitMonthPlanModel.findOne({userId:m.userid,month:m.month,consumerCode:m.storeCode,planDate:m.planDate,plantype:m.planType})
                console.log(c_data,'c_data,c_data',index,'>>?>>>>>>>>',m.storeName,m.userid)
                if(!c_data){
                    unplanList.push({id:m._id})
                }
                if(c_data){
                    let up=await visitMonthPlanModel.update({_id:c_data._id},{visitRecoder:m._id})
                    console.log(up,'upupupupupupupup');
                    let up2=await storeRecoderModel.update({_id:m._id},{planId:c_data._id})
                    console.log(up2,'up2up2up2up2up2up2up2up2');
                }
        
                
            }
        }
        console.log(unplanList,',unplanList_unplanList')

        // let r_data=await storeRecoderModel.find({month:'04'});
        // console.log(r_data,'r_data')

        // let c_data=await visitMonthPlanModel.findOne({planStatus:'写好拜访记录',month:'04'})
        // console.log(c_data,'c_data')
        // let r_data=await storeRecoderModel.find({userid:c_data.userId,month:'04',planDate:c_data.planDate,storeCode:c_data.consumerCode});
        // console.log(r_data,'r_data')


        // let c_data=await visitMonthPlanModel.findOne({planStatus:'写好拜访记录',month:'04'})
        // console.log(c_data,'c_data')
        // let r_data=await storeRecoderModel.findById({_id:'5bd11e738334ba1de45f6abb'});
        // console.log(r_data,'r_data')
        // let c_data=await visitMonthPlanModel.findOne({userId:r_data.userid,month:r_data.month,consumerCode:r_data.storeCode,planDate:r_data.planDate})
        // console.log(c_data,'c_data')

        // let m_data=await visitMonthPlanModel.find({plantype:r_data[1].planType,planDate:r_data[1].planDate,userId:r_data[1].userid,consumerCode:r_data[1].storeCode})
        // console.log(m_data,'m_data')
        // if(m_data.length==0){
        //     let info=await dTalkApi.getUserDetail(r_data[1].userid)
        //     console.log(info,'_info')
        // }
       
    }


    async plan_consumerRecoder(){
       /*  let monthList=await consumerRecoderModel.distinct('month',{comment:2});
        console.log(monthList,'<------monthList'); */
      /*   let b=await consumerRecoderModel.update({comment:1},{planId:''})
        console.log(b,'bbbbbbbbbbbb') */
        let monthList=await consumerRecoderModel.distinct('planType',{comment:2});//      现有客户  其他  新拓展客户
        console.log(monthList,'<------monthList');
        let unplanList:any[]=[]//有记录没有对应的计划的
         let r_data2=await consumerRecoderModel.find({month:'04',comment:2,planType:'新拓展客户'});//10月份的另外   其他,,   ,,planDate:{"$lte":'2018-10-24'
        
        if(r_data2.length>0){
            for (let index = 0; index < r_data2.length; index++) {
                let m = r_data2[index];
                let c_data=await visitMonthPlanModel.findOne({userId:m.userid,consumerName:m.consumerName,consumerCode:m.consumerCode,month:m.month,planDate:m.planDate,plantype:m.planType})
                console.log(c_data,'c_data,c_data',index,'>>?>>>>>>>>',m.consumerCode,m.consumerName,m.userid)
                if(!c_data){
                    unplanList.push({id:m._id})
                }
                if(c_data){
                    let up=await visitMonthPlanModel.update({_id:c_data._id},{visitRecoder:m._id})
                    console.log(up,'upupupupupupupup');
                    let up2=await consumerRecoderModel.update({_id:m._id,comment:2},{planId:c_data._id})
                    console.log(up2,'up2up2up2up2up2up2up2up2');
                }
            }
        }
        console.log(unplanList,',unplanList_unplanList',unplanList.length)
    }

    /* 
    async getSign(nonceStr: string, timeStamp: number, origUrl: string) {
        let ticket = await this.getJsapi_ticket();
        //   corp.expires
        var origUrlObj = url.parse(origUrl);
        delete origUrlObj['hash'];
        var newUrl = url.format(origUrlObj);
        var plain = 'jsapi_ticket=' + ticket +
            '&noncestr=' + nonceStr +
            '&timestamp=' + timeStamp +
            '&url=' + newUrl;

        console.log(plain,'<-p-----------plain');
        var sha1 = crypto.createHash('sha1');
        sha1.update(plain, 'utf8');
        var signature = sha1.digest('hex');
        console.log(signature,'signature_signature_signature_')
        return signature;

    }
    */

    async encryptData(json:any){
        //console.log(json,'<---加密的传输数据')
        if(json){
            let key=await test3Service.key();
            let token=await test3Service.token();
            let sign_=await test3Service.sign(null,token,json.digest,json.time);
            let s_new=await test3Service.upToLower(json.sign);
            if(sign_==s_new){  //验证签名
                let json2=await rp.post(`http://localhost:8081/api/senddata2`,{json:true});
                if(json2&&json2.data){
                    return json2.data;
                }
            }
        }
        return null;

    }
    //字符串大写转换成小写    字符串由字母和数字组成
    async upToLower(str:any){
        let s_arr=str.split('');
        let s_new:any='';
        let newArr:any[]=['0','1','2','3','4','5','6','7','8','9'];
        s_arr.forEach(element => {
            if(newArr.includes(element)){
                s_new+=element;
            }else{
                let ele= element.toLowerCase();
                s_new+=ele;
            }
        });
        //console.log(s_new,'s_news_news_news_news_news_new')
        return s_new;
    }

    async key(){
        return 'fdafsji3829AENG7hfdihf66112374jrj2k4jHJkjk3';
    }

    async token(){
        return '#@!1234567890!@#';
    }

    async sign(appid:string,token:any,digest:any,timestamp:any){
        if(!appid){
            appid='send_data2';
        }
        let sign:any=''
        if(appid&&token&&timestamp){
           let plain=appid+digest+timestamp+token;
            sign=await test3Service.digest(plain,'sha1');
        }
        return sign;
    }

    async digest(strSrc:string,encName:string){
        if (encName == null || encName=='') {
            encName = "MD5";
        }
        let sha1 = crypto.createHash(encName);
        sha1.update(strSrc);
        let strDes = sha1.digest('hex');
        return strDes;
    }

    async  getUserIdByCode(code: string, cache: any,access_token:string) {
        if (cache[code]) {
            return cache[code]
        } else {
            let result = await dTalkApi.getDingDingAdressList(code,access_token);
            if (!result || !result.list || result.list.length == 0) {
                console.log("工号", code, "未找到");
                return null;
            }
            cache[code] = result.list[0];
            console.log(cache[code])
            return cache[code]
        }
    }


}
let test3Service=new TestService();
export default test3Service;